import * as type from '../mutation'

const state = {
	added:[],
	checkoutStauts:null
}

const mutations = {
	[type.ADD_TO_CART] (state,{id}){
		state.lastCheckout = null;
		const recode = state.added.find( p => p.id === id);
		if(!recode){
			state.added.push({
				id,
				quantity:1
			})
		}else{
			recode.quantity++;
		}
	},
	[type.REMOVE_FROM_CART] (state,{id,quantity}){
		for (let  i= 0; i < state.added.length; i++) {
			if(id === state.added[i].id){
				state.added.splice(i,1);
				break;
			}
		}
	}
}

export default {
  state,
  mutations
}